package com.example.gitpart03;

//zheshi
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.example.gitpart03.BookListAdapter;

import java.util.ArrayList;
import java.util.List;

public class BookListActivity extends AppCompatActivity {

    private RecyclerView recyclerViewBooks;
    private BookListAdapter adapter;
    private List<Book> bookList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_list);

        // 初始化视图
        recyclerViewBooks = findViewById(R.id.recyclerViewBooks);
        recyclerViewBooks.setLayoutManager(new LinearLayoutManager(this));

        // 初始化数据
        initBooks();

        // 设置适配器
        adapter = new BookListAdapter(this, bookList);
        recyclerViewBooks.setAdapter(adapter);

        // 设置项目点击监听
        adapter.setOnItemClickListener(new BookListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                // 启动阅读活动，传递书籍信息
                Book selectedBook = bookList.get(position);
                Intent intent = new Intent(BookListActivity.this, BookReaderActivity.class);
                intent.putExtra("book", selectedBook);
                intent.putExtra("chapter", 1); // 默认从第一章开始
                startActivity(intent);
            }
        });
    }

    private void initBooks() {
        bookList = new ArrayList<>();

        // 添加示例书籍
        bookList.add(new Book("1", "三国演义", "罗贯中",
                "https://picsum.photos/id/24/200/300",
                "《三国演义》是中国第一部长篇章回体历史演义小说...", 120));

        bookList.add(new Book("2", "红楼梦", "曹雪芹",
                "https://picsum.photos/id/25/200/300",
                "《红楼梦》，中国古典四大名著之首...", 120));

        bookList.add(new Book("3", "西游记", "吴承恩",
                "https://picsum.photos/id/26/200/300",
                "《西游记》是中国古代第一部浪漫主义章回体长篇神魔小说...", 100));

        bookList.add(new Book("4", "水浒传", "施耐庵",
                "https://picsum.photos/id/27/200/300",
                "《水浒传》，中国四大名著之一，是一部以宋朝为背景的长篇小说...", 120));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            // 这里可以添加设置功能
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}